<template>
    <div class="app-container">
        <div class="top">
            <div>
                <img src="../assets/images/deviceInfoTitle.png" alt="">
            </div>
            <div>仓库管理</div>
        </div>
        <div class="container">
            <div class="search">
                <div class="search-buttons"> <el-button type="primary" size="mini" @click="handleAdd"
                        v-hasPermi="['warehouse:warehouse:add']">新增</el-button>
                    <!-- <el-button type="danger" size="mini" :disabled="multiple" @click="handleDelete"
                        v-hasPermi="['warehouse:warehouse:delete']">删除</el-button> -->
                </div>
                <div class="search-form">
                    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
                        <el-form-item label="仓库编码" prop="warehouseCode">
                            <el-input v-model="queryParams.warehouseCode" placeholder="请输入仓库编码" clearable
                                @keyup.enter.native="handleQuery" />
                        </el-form-item>
                        <el-form-item label="仓库名称" prop="warehouseName">
                            <el-input v-model="queryParams.warehouseName" placeholder="请输入仓库名称" clearable
                                @keyup.enter.native="handleQuery" />
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" icon="el-icon-search" size="mini"
                                @click="handleQuery">查询</el-button>
                            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                        </el-form-item>
                    </el-form>
                </div>
            </div>
            <el-divider></el-divider>
            <div class="table-container">

                <el-table v-loading="loading" :data="configList" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55" align="center" />
                    <el-table-column label="序号" align="center" type="index" width="55" />
                    <el-table-column label="仓库编码" align="center" prop="warehouseCode" :show-overflow-tooltip="true" />
                    <el-table-column label="仓库名称" align="center" prop="warehouseName" :show-overflow-tooltip="true" />
                    <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                        <template slot-scope="scope">
                            <el-button size="mini" type="text" @click="handleUpdate(scope.row)"
                                v-hasPermi="['warehouse:warehouse:edit']">修改</el-button>
                            <el-button size="mini" type="text" @click="handleDelete(scope.row)"
                                v-hasPermi="['warehouse:warehouse:delete']">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize" @pagination="getList" />
        </div>

        <!-- 添加或修改参数配置对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="仓库编码" prop="warehouseCode">
                    <el-input v-model="form.warehouseCode" placeholder="请输入" />
                </el-form-item>
                <el-form-item label="仓库名称" prop="warehouseName">
                    <el-input v-model="form.warehouseName" placeholder="请输入" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listWarehouse, getWarehouse, delWarehouse, addWarehouse, updateWarehouse } from "@/api/warehouse"

export default {
    name: "Warehouse",
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示查询条件
            showSearch: true,
            // 总条数
            total: 0,
            // 参数表格数据
            configList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 日期范围
            dateRange: [],
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                warehouseCode: undefined,
                warehouseName: undefined
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                warehouseCode: [
                    { required: true, message: "仓库编码不能为空", trigger: "blur" }
                ],
                warehouseName: [
                    { required: true, message: "仓库名称不能为空", trigger: "blur" }
                ]
            }
        }
    },
    created() {
        this.getList()
    },
    methods: {
        /** 查询参数列表 */
        getList() {
            this.loading = true
            listWarehouse(this.queryParams).then(response => {
                this.configList = response.rows
                this.total = response.total
                this.loading = false
            }
            )
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.form = {
                warehouseId: undefined,
                warehouseCode: undefined,
                warehouseName: undefined
            }
            this.resetForm("form")
        },
        /** 查询按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.dateRange = []
            this.resetForm("queryForm")
            this.handleQuery()
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset()
            this.open = true
            this.title = "添加仓库"
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.warehouseId)
            this.single = selection.length != 1
            this.multiple = !selection.length
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset()
            const warehouseId = row.warehouseId || this.ids
            getWarehouse(warehouseId).then(response => {
                this.form = response.data
                this.open = true
                this.title = "修改仓库"
            })
        },
        /** 提交按钮 */
        submitForm: function () {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.warehouseId != undefined) {
                        updateWarehouse(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功")
                            this.open = false
                            this.getList()
                        })
                    } else {
                        addWarehouse(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功")
                            this.open = false
                            this.getList()
                        })
                    }
                }
            })
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const warehouseId = row.warehouseId || this.ids
            this.$modal.confirm('是否确认删除数据项？').then(function () {
                return delWarehouse(warehouseId)
            }).then(() => {
                this.getList()
                this.$modal.msgSuccess("删除成功")
            }).catch(() => { })
        }
    }
}
</script>
<style scoped lang="scss">
.app-container {
    width: 100%;
    height: calc(100vh - 60px);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    padding-bottom: 10px;
}

.container {
    width: 100%;
    margin-top: 16px;
    background: #fff;
    padding: 24px 24px 0px 24px;
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;

    .el-divider {
        margin: 20px 0 !important;
    }

    .search {
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        gap: 16px;
        margin-bottom: 16px;
        justify-content: space-between;

        .search-buttons {
            flex-shrink: 0;
        }

        .search-form {
            flex: 1;
            min-width: 0;
            display: flex;
            justify-content: flex-end;

            /* 让表单内容靠右 */
            ::v-deep .el-form {
                display: flex;
                flex-wrap: wrap;
                align-items: center;
                gap: 16px;
                margin: 0;
                padding: 0;
                justify-content: flex-end;
                /* 让表单元素从右向左排列 */
            }

            ::v-deep .el-form-item {
                margin: 0;
                flex-shrink: 0;
            }

            ::v-deep .el-input {
                width: 160px;
                height: 32px;
                line-height: 32px;
            }

            ::v-deep .el-form-item__label {
                font-family: PingFang SC;
                font-weight: 400;
                font-size: 14px;
                height: 32px;
                line-height: 32px;
                letter-spacing: 0%;
                vertical-align: middle;
                color: #313D4B;
            }
        }
    }

    .table-container {
        flex: 1;
        overflow: auto;
    }
}

.top {
    width: 100%;
    height: 56px;
    border-radius: 4px;
    background: rgba(255, 255, 255, 1);
    display: flex;
    align-items: center;
    padding: 0 16px;

    >div:first-child {
        width: 24px;
        height: 24px;
        display: flex;
        align-items: center;
        justify-content: center;
        position: relative; // 新增

        img {
            width: 100%;
            height: 100%;
            object-fit: contain;
            position: relative;
            top: 2px; // 微调图片垂直位置
        }
    }

    >div:last-child {
        font-family: PingFang SC;
        margin-left: 8px;
        font-weight: 500;
        font-size: 18px;
        color: rgba(29, 33, 41, 1);
        line-height: 24px;
        height: 24px;
        display: flex;
        align-items: center;
    }
}
</style>
